/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.murcanlat.webcan.html.binders.impl;

import com.murcanlat.example.models.PersonelModel;
import com.murcanlat.webcan.Template;
import com.murcanlat.webcan.binders.interfaces.CompilerServis;
import com.murcanlat.webcan.exceptions.AnnotationException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Murat CANPOLAT <murat.canpolat@gebze.bel.tr>
 */
public class CompilerServisImpl extends HashMap<String, Object>
        implements CompilerServis {

    private final Template baseTemplate;

    public CompilerServisImpl(Template baseTemplate) {
        this.baseTemplate = baseTemplate;
    }

    public Object get(String key) {
        Object result = null;
        String[] fieldKeys = key.split("\\.");
        try {

            System.out.println(key);
            Class template = baseTemplate.getClass();
            int fieldSize = fieldKeys.length;
            for (int i = 0; i < fieldSize; i++) {
                String str = fieldKeys[i];
                Field field = template.getDeclaredField(str);
                if (Modifier.isFinal(field.getModifiers())) {
                    continue;
                }
                if (i == fieldSize - 1) {
                    if (field.getType().getSuperclass().equals(Template.class)) {
                        template = field.getType();
                        result = copilerTemplate(template, key);
                    }
                } else {
                    template = field.getType();
                }
            }

        } catch (AnnotationException ex) {
            Logger.getLogger(CompilerServisImpl.class.getName()).log(Level.CONFIG, null, ex);
        } catch (NoSuchFieldException ex) {
            Logger.getLogger(CompilerServisImpl.class.getName()).log(Level.CONFIG, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(CompilerServisImpl.class.getName()).log(Level.CONFIG, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(CompilerServisImpl.class.getName()).log(Level.INFO, null, ex);
        }
        return result;
    }

    public static void main(String[] args) {
        CompilerServis webCanServis = new CompilerServisImpl(new PersonelModel());
        //  webCanServis.get(PersonelModel.UNVAN_MODEL + "." + UnvanModel.UNVAN_KODU);
        //  webCanServis.get(PersonelModel.UNVAN_MODEL + "." + UnvanModel.UNVAN_ACIKLAMA);
        webCanServis.get(PersonelModel.UNVAN_MODEL);
        //  webCanServis.get(PersonelModel.ADI);
    }

    private Object copilerTemplate(Class template, String str) {
        Object result = null;
        do {
            Field[] fields = template.getDeclaredFields();
            for (Field field : fields) {
                if (Modifier.isFinal(field.getModifiers())) {
                    continue;
                }
                if (field.getType().getSuperclass().equals(Template.class)) {
                    template = field.getType();
                    result = copilerTemplate(template, str);
                } else {
                    Annotation[] annotations = field.getDeclaredAnnotations();
                    for (Annotation annotation : annotations) {
                        // DATABASE CONNECTİON
                        System.out.println(str+"."+field.getName() + " annot " + annotation.annotationType().getSimpleName());
                    }
                    template = null;
                }
            }

        } while (template != null);

        return result;
    }
}
